﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LabMS.BLL;
using LabMS.Model;
using System.Collections.Generic;
using ExtendBLL;
using System.IO;

namespace LabMS.Web.Equipment.Consum
{
    public partial class consumedit : Base.BasePage
    {
        public string strID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("HCGL");//必须写在ispostback外
            strPageUserType.Add("mana");
            if (Request.QueryString["id"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            strID = Request.QueryString["id"].ToString();
            if (!Page.IsPostBack)
            {

                ExtendModule.InstrumentAndConsumables mconsum = checkQueryString(strID);
                if (mconsum == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "字典类别不存在!");
                    return;
                }
                bindDDL(mconsum);
                bind(mconsum);
                tbBuyDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
                tbProduceDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
                tbStateCode.Style.Add("display", "none");

                tbCNub.Attributes.Add("readOnly", "-1");
                tbCountry.Attributes.Add("readOnly", "-1");
                tbBuyDate.Attributes.Add("readOnly", "-1");
                tbProduceDate.Attributes.Add("readOnly", "-1");
                LabID.Value = mconsum.ID.ToString();
            }

        }

        ExtendModule.InstrumentAndConsumables checkQueryString(string strID)
        {
            ExtendBLL.InstrumentAndConsumables bconsum = new ExtendBLL.InstrumentAndConsumables();
            string strWhere = " Type = 'xiaohaopin' and ID =" + strID;
            List<ExtendModule.InstrumentAndConsumables> listconsum = bconsum.GetExtendModelList(strWhere);
            if (listconsum.Count <= 0)
            {
                return null;
            }
            return listconsum[0];
        }

        public void bindDDL(ExtendModule.InstrumentAndConsumables mconsum)
        {
            labelCreatDate.Text = DateTime.Today.ToShortDateString();
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetItemListByDClass("haocaileibie");
            ddlCClass.DataSource = listCClass;
            ddlCClass.DataTextField = "Name";
            ddlCClass.DataValueField = "Code";
            ddlCClass.DataBind();
            ddlCClass.SelectedValue = mconsum.ClassNumber;

            listCClass = eDictionary.GetItemListByDClass("shebeilaiyuan");
            ddlComeFrom.DataSource = listCClass;
            ddlComeFrom.DataTextField = "Name";
            ddlComeFrom.DataValueField = "Code";
            ddlComeFrom.DataBind();
            ddlComeFrom.SelectedValue = mconsum.Source;

            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();
            ddlArrow.SelectedValue = mconsum.Arrow;

            listCClass = eDictionary.GetItemListByDClass("jingfeikemu");
            ddlFundsSubject.DataSource = listCClass;
            ddlFundsSubject.DataTextField = "Name";
            ddlFundsSubject.DataValueField = "Code";
            ddlFundsSubject.DataBind();
            ddlFundsSubject.SelectedValue = mconsum.FundsSubject;

            listCClass = eDictionary.GetItemListByDClass("dangwei");
            ddlModle.DataSource = listCClass;
            ddlModle.DataTextField = "Name";
            ddlModle.DataValueField = "Code";
            ddlModle.DataBind();
            ddlModle.SelectedValue = mconsum.Measurement;

            tbLocation.Text = mconsum.LabName;
            tbLocationCode.Text  = mconsum.StockLocation;
        }
        void bind(ExtendModule.InstrumentAndConsumables mconsum)
        {
            EquipPhoto.ImageUrl = "~/Equipment/PicShow.aspx?ID=" + mconsum.ID;
            tbClassNub.Text = mconsum.ClassNumber.ToString();
            tbCName.Text = mconsum.Name;
            tbCNub.Text = mconsum.Numbers;
            tbCountry.Text = mconsum.CountryCode;

            tbDocuments.Text = mconsum.Documents.ToString();
            tbFNub.Text = mconsum.FactoryCode.ToString();
            tbNub.Text = mconsum.Quantity.ToString();
            tbPrice.Text = mconsum.UnitPrice;
            tbPurpose.Text = mconsum.Purpose;
            try
            {
                tbProduceDate.Text = mconsum.ProductionDate.Value.ToShortDateString();
            }
            catch { }
            try
            {
                labelCreatDate.Text = mconsum.CreatedDate.Value.ToShortDateString();
            }
            catch { }
            try
            {
                tbBuyDate.Text = mconsum.PurchaseDate.Value.ToShortDateString();
            }
            catch { }
            tbTypeNub.Text = mconsum.Model;
            tbVENDOR.Text = "";//应加上
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {

            #region 验证
           
            if (string.IsNullOrEmpty(tbCName.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "耗材名称不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbTypeNub.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "型号不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbNub.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "数量不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbPrice.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "单价不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbBuyDate.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "购置日期不能为空!");
                return;
            }

            #endregion
            lbErr.Visible = false;
            Model.InstrumentAndConsumables consumables = new LabMS.Model.InstrumentAndConsumables();
            BLL.InstrumentAndConsumables bconsumables = new LabMS.BLL.InstrumentAndConsumables();
            consumables = bconsumables.GetModel(long.Parse(LabID.Value));
            consumables.Arrow = ddlArrow.SelectedValue;
            consumables.ClassNumber = ddlCClass.SelectedValue;
            consumables.CountryCode = tbCountry.Text;
            consumables.Documents = tbDocuments.Text.Trim();
            consumables.FactoryCode = tbFNub.Text.Trim();
            consumables.FundsSubject = ddlFundsSubject.SelectedValue;
            consumables.Measurement = ddlModle.SelectedValue;
            consumables.Model = tbTypeNub.Text.Trim();
            consumables.Name = tbCName.Text.Trim();
            consumables.Numbers = tbCNub.Text.Trim();
            consumables.OperatorDate = DateTime.Now;
            DateTime tempTime = new DateTime();
            if (DateTime.TryParse(tbProduceDate.Text.Trim(), out tempTime))
            {
                consumables.ProductionDate = tempTime;
            }
            if (DateTime.TryParse(tbBuyDate.Text.Trim(), out tempTime))
            {
                consumables.PurchaseDate = tempTime;
            }
            if (DateTime.TryParse(labelCreatDate.Text.Trim(), out tempTime))
            {
                consumables.CreatedDate = tempTime;
            }
            decimal iTemp = 0;
            if (decimal.TryParse(tbNub.Text.Trim(), out iTemp))
            {
                consumables.Quantity = iTemp;
            }
            consumables.Source = ddlComeFrom.SelectedValue;
            consumables.SpaceLocation = tbLocationCode.Text;
            consumables.StockLocation = tbLocationCode.Text;
            consumables.UnitPrice = tbPrice.Text.Trim();
            consumables.Purpose = tbPurpose.Text.Trim();
            //修改图片
            try
            {
                if (imageUpload.HasFile)
                {
                    HttpPostedFile upPhoto = imageUpload.PostedFile;
                    int upPhotoLength = upPhoto.ContentLength;
                    string PhotoContentType = upPhoto.ContentType;
                    byte[] PhotoArray = new byte[upPhotoLength];
                    Stream PhotoStream = upPhoto.InputStream;
                    PhotoStream.Read(PhotoArray, 0, upPhotoLength);

                    consumables.Pic = PhotoArray;//相片
                    consumables.PicType = PhotoContentType;//图片类型
                }
            }
            catch
            {
                LabMS.Common.JShelper.JSAlert(Page, "Picwrong", "程序错误:图片保存出错");
            }
            //无供应商
            try
            {
                bconsumables.Update(consumables);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "修改成功!", "consumedit.aspx?ID=" + LabID.Value);
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message.ToString();
            }
            
        }
    }
}
